package cn.java79.springboot.ctrl;

import cn.java79.springboot.zsb.entity.User;
import cn.java79.springboot.pojo.AjaxPage;
import cn.java79.springboot.zsb.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @author oldliu
 * @since 1.0
 */
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("ureg")
    public String userreg(User user,
                          @RequestParam(name = "headpic2", required = false) MultipartFile file, RedirectAttributes rd, HttpServletRequest request) throws IOException {
        if (!file.isEmpty()) {
            //选了文件,获得存放的目录，不存在则创建
            String imgs = request.getServletContext().getRealPath("imgs");
            File f = new File(imgs);
            if (!f.exists())
                f.mkdir();
            //获得文件名
            String oldName = file.getOriginalFilename();
            //拿到后缀
            String suffix = oldName.substring(oldName.lastIndexOf('.'));
            String newName = UUID.randomUUID() + suffix;
            file.transferTo(new File(f, newName));//转移到目录下
            user.setHeadpic("imgs/" + newName);//保存新名称和路径 到对象中
        }
        this.userService.save(user);
        //显示一遍后自动删除
        rd.addFlashAttribute("msg", "新增学员成功");
        return "redirect:/user/search";
    }

    //删除
    @GetMapping("del-{id}")
    public String del(@PathVariable("id") Long id, RedirectAttributes rd) {
        int r = this.userService.del(id);
        rd.addFlashAttribute("msg", r > 0 ? "删除成功" : "删除失败");
        return "redirect:/user/search";
    }

    //修改
    @GetMapping("to-{id}")
    public String toUpdate(@PathVariable("id") Long id, Model m) {
        User user = this.userService.get(id);
        m.addAttribute("u", user);
        return "updateUser";
    }

    //update/100,
    @PostMapping("update-{id}")
    public String update(@PathVariable("id") Long id, User user) {
        user.setId(id);
        this.userService.update(user);
        //未完整
        return "xxxxx";
    }

    @GetMapping("search")
    public AjaxPage<User> search(String loginname, String realname, String phone, @RequestParam(name = "pageNo", defaultValue = "1") int pageNo) {
        AjaxPage<User> p = this.userService.search(loginname, realname, phone, pageNo);
        return p;
    }
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        //true:允许输入空值，false:不能为空值
        //第二个参数，请求参数名
        binder.registerCustomEditor(Date.class, "enterdate", new CustomDateEditor(dateFormat, true));

        SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
        dateFormat2.setLenient(false);
        binder.registerCustomEditor(Date.class, "birthday", new CustomDateEditor(dateFormat2, true));
        // binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

}
